<template>
  <div class="login-container">
    <div class="login-box">
      <el-form ref="loginFormRef"  :model="loginForm" :rules="loginFormRules" label-width="0px" class="login-form">
        <el-form-item  prop="phone" >
          <el-input v-model="loginForm.phone" placeholder="手机号" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input v-model="loginForm.password" placeholder="密码" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns" >
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import sysuserApi from "@/api/sysuser";
export default {
  name: "Login",
  data() {
    return{
      codeBtnText:'获取验证码',
      // 登录表单数据绑定对象
      loginForm:{
        phone:'',
        password:'',
      },
      // 表单验证规则对象
      loginFormRules:{
        phone:[{ required: true, message: '请输入手机号', trigger: 'blur' }],
        password:[
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  created() {
  },
  methods:{
    //清空登录表单内容
    resetLoginForm(){
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate( async (valid) => {
        if(!valid) return;
        const {data:res} =await sysuserApi.login(this.loginForm);

        if(res.code === 200){
          this.$message.success('登录成功')
          //将用户信息保存到vuex中
          this.$store.commit('setUser',res.data)
          await this.$router.push('/home')
        }else{
          return this.$message.error("登录失败")
        }
      })
    },
  }
}
</script>

<style lang="less" scoped>
.login-container{
  background-color: rgba(42, 121, 38, 0.99);
  height: 100%;
}
.login-box {
  width: 450px;
  height: 240px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;

    transform: translate(-50%, -50%);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color:#eee;
    }
  }

  .login-form{
    position: absolute;
    bottom: 0;
    width: 100%;
    // 上下0，左右
    padding: 0 20px;
    box-sizing: border-box;
  }
}
.btns{
  display: flex;
  // 横轴居右对齐
  justify-content: flex-end;
}
</style>